Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1

    Public CurWord As String = ""
    Public CorrectGuesses As Integer = 0
    Public BadGuesses As Integer = 0

    Public LstLetters As New List(Of String)
    Public Alpha As Integer = 0
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim bmp As New Bitmap(pb2.Width, pb2.Height)
        If pb2.Image Is Nothing Then
        Else : bmp = pb2.Image
        End If
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.SmoothingMode = SmoothingMode.HighQuality

        Dim p1 As New Pen(Color.FromArgb(Alpha, Color.Red), 2)
        Select Case BadGuesses
            Case 1
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
            Case 2
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
            Case 3
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
                g.DrawLine(p1, 25, 15, bmp.Width - 50, 15)
            Case 4
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
                g.DrawLine(p1, 25, 15, bmp.Width - 50, 15)
                g.DrawLine(p1, bmp.Width - 50, 15, bmp.Width - 50, 30)
            Case 5
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
                g.DrawLine(p1, 25, 15, bmp.Width - 50, 15)
                g.DrawLine(p1, bmp.Width - 50, 15, bmp.Width - 50, 30)
                g.DrawArc(p1, bmp.Width - 75, 30, 50, 50, 0, 360)
            Case 6
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
                g.DrawLine(p1, 25, 15, bmp.Width - 50, 15)
                g.DrawLine(p1, bmp.Width - 50, 15, bmp.Width - 50, 30)
                g.DrawArc(p1, bmp.Width - 75, 30, 50, 50, 0, 360)
                g.DrawLine(p1, bmp.Width - 50, 80, bmp.Width - 50, bmp.Height - 35)
            Case 7
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
                g.DrawLine(p1, 25, 15, bmp.Width - 50, 15)
                g.DrawLine(p1, bmp.Width - 50, 15, bmp.Width - 50, 30)
                g.DrawArc(p1, bmp.Width - 75, 30, 50, 50, 0, 360)
                g.DrawLine(p1, bmp.Width - 50, 80, bmp.Width - 50, bmp.Height - 35)
                g.DrawLine(p1, bmp.Width - 50, 100, bmp.Width - 70, 80)
                g.DrawLine(p1, bmp.Width - 50, 100, bmp.Width - 30, 80)
            Case 8
                p1.Color = Color.Red
                Alpha = 0
                g.DrawLine(p1, 5, bmp.Height - 3, bmp.Width - 5, bmp.Height - 3)
                g.DrawLine(p1, 25, bmp.Height - 3, 25, 15)
                g.DrawLine(p1, 25, 15, bmp.Width - 50, 15)
                g.DrawLine(p1, bmp.Width - 50, 15, bmp.Width - 50, 30)
                g.DrawArc(p1, bmp.Width - 75, 30, 50, 50, 0, 360)
                g.DrawLine(p1, bmp.Width - 50, 80, bmp.Width - 50, bmp.Height - 35)
                g.DrawLine(p1, bmp.Width - 50, 100, bmp.Width - 70, 80)
                g.DrawLine(p1, bmp.Width - 50, 100, bmp.Width - 30, 80)
                g.DrawLine(p1, bmp.Width - 50, bmp.Height - 35, bmp.Width - 70, bmp.Height - 15)
                g.DrawLine(p1, bmp.Width - 50, bmp.Height - 35, bmp.Width - 30, bmp.Height - 15)

                'Panel1.Visible = True
                'Button1.Enabled = False
                'ListBox1.Enabled = False
                'Timer1.Enabled = False
                'FinishMessages.lose(CorrectGuesses, BadGuesses, CurWord)

        End Select
        Alpha = Alpha + 1
        If Alpha = 255 Then
            Alpha = 0


        End If
        pb2.Image = bmp
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each s As String In ListBox1.Items
            LstLetters.Add(s)
        Next
        DrawBack()
    End Sub

    ''' <summary>
    ''' draws the background graphics
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub DrawBack()
        Dim bmp As New Bitmap(Me.Width, Me.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim b1 As New LinearGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), Color.Black, Color.White, LinearGradientMode.ForwardDiagonal)
        Dim c As New ColorBlend
        c.Colors = New Color() {Color.DarkGreen, Color.SkyBlue, Color.Blue, Color.MidnightBlue}
        c.Positions = New Single() {0.0, 0.3, 0.6, 1.0}
        b1.InterpolationColors = c
        g.FillRectangle(b1, New Rectangle(0, 0, Me.Width, Me.Height))
        NewWord()

        Dim bmp1 As New Bitmap(MenuStrip1.Width, MenuStrip1.Height)
        Dim g1 As Graphics = Graphics.FromImage(bmp1)
        Dim c1 As New ColorBlend
        c1.Colors = New Color() {Color.Transparent, Color.FromArgb(150, Color.Green), Color.Transparent, Color.Transparent}
        c1.Positions = New Single() {0.0, 0.25, 0.8, 1.0}
        Dim b2 As New LinearGradientBrush(New Rectangle(0, 0, MenuStrip1.Width, MenuStrip1.Height), Color.Black, Color.White, LinearGradientMode.ForwardDiagonal)
        b2.InterpolationColors = c1
        g1.FillRectangle(b2, New Rectangle(0, 0, MenuStrip1.Width, MenuStrip1.Height))
        MenuStrip1.BackgroundImage = bmp1
        Me.BackgroundImage = bmp

        Dim bmp2 As New Bitmap(pb2.Width, pb2.Height)
        Dim g2 As Graphics = Graphics.FromImage(bmp2)
        Dim path As New GraphicsPath
        path.AddRectangle(New Rectangle(0, 0, bmp2.Width, bmp2.Height))
        Dim b3 As New PathGradientBrush(path)
        b3.CenterPoint = New Point(bmp2.Width, 0)
        b3.CenterColor = Color.Yellow
        b3.SurroundColors = New Color() {Color.Black}
        g2.FillRectangle(b3, New Rectangle(0, 0, bmp2.Width, bmp2.Height))
        pb2.BackgroundImage = bmp2
    End Sub

    ''' <summary>
    ''' randomizes a new word to use
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub NewWord()
        CurWord = ""
        CorrectGuesses = 0
        BadGuesses = 0
        pb2.Image = Nothing
        txtWord.Visible = False
        Panel1.Visible = False
        ListBox1.Items.Clear()
        For Each s As String In LstLetters
            ListBox1.Items.Add(s)
        Next
        Button1.Enabled = False
        ListBox1.Enabled = True
        Dim a As New Dictionary.CDict
        a.DictNum = My.Settings.DictNum
        Randomize() '(Rnd(Now.Ticks) * 150)
        Dim r As New Random '(Rnd(Now.Millisecond) * Now.Second / Now.Ticks)
        length = r.Next(My.Settings.MinWords, My.Settings.MaxWords + 1)
        'MsgBox(length)
        search = ""

        For ii = 1 To length
            search = search & "?"
        Next
        If search.length Then
            search = Mid(search, 1, 28)
        End If
        a.FindMatches(search)
        Dim lst As New List(Of String)
        For Each w As String In a.Words
            lst.Add(w)

        Next
        r = New Random '(Rnd(Now.ToBinary) * 10000)
        itm = r.Next(0, lst.Count)
        CurWord = lst.Item(itm)
        lst.Clear() 'clear up space in memory
        DrawBlank()
        txtWord.Text = CurWord
        lbltotalletters.Text = CurWord.Length
    End Sub

    ''' <summary>
    ''' draws the image that contains the blanks for the word
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub DrawBlank()
        Dim bmp As New Bitmap(CurWord.Length * 25 - 5, pb1.Height)
        pb1.Size = bmp.Size
        pb1.Location = New Point((Me.Width / 2) - (pb1.Width / 2), pb1.Location.Y)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim p1 As New Pen(Color.Black, 3)

        For ii = 1 To CurWord.Length
            g.DrawLine(p1, 25 * (ii - 1), pb1.Height - 5, (25 * (ii - 1)) + 20, pb1.Height - 5)

        Next
        pb1.Image = bmp
    End Sub

    ''' <summary>
    ''' draws the selected letter in the correct locations on the image
    ''' </summary>
    ''' <param name="letter"></param>
    ''' <remarks></remarks>
    Public Sub DrawLetter(ByVal letter As Char)
        If CurWord.Contains(letter) = True Then

        Else : BadGuesses += 1
            Timer1.Enabled = True
            Exit Sub
        End If
        Dim bmp As Bitmap = pb1.Image
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim f1 As New Font(System.Drawing.FontFamily.GenericSerif, 25, FontStyle.Bold, GraphicsUnit.Pixel)
        g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
        For ii = 1 To CurWord.Length
            aaa = Mid(CurWord, ii, 1)
            If aaa = letter Then
                CorrectGuesses += 1
                g.DrawString(letter, f1, Brushes.Black, 25 * (ii - 1), pb1.Height - 35)

            End If
        Next

        pb1.Image = bmp
    End Sub

    ''' <summary>
    ''' select a letter
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        aaa = ListBox1.SelectedIndex
        txt = ListBox1.Items.Item(aaa)
        txt = LCase(txt)
        ListBox1.Items.RemoveAt(aaa)
        ListBox1.Items.Insert(aaa, "__")
        DrawLetter(txt)
        Button1.Enabled = False
        If CorrectGuesses = CurWord.Length Then
            Panel1.Visible = True
            Button1.Enabled = False
            ListBox1.Enabled = False
            FinishMessages.Win(CorrectGuesses, BadGuesses, CurWord)
            RecordsFRM.Show()
        ElseIf BadGuesses >= 8 Then
            Panel1.Visible = True
            Button1.Enabled = False
            ListBox1.Enabled = False
            FinishMessages.lose(CorrectGuesses, BadGuesses, CurWord)
            RecordsFRM.Show()
        End If
    End Sub

    ''' <summary>
    ''' select a letter
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        aaa = ListBox1.SelectedIndex
        txt = ListBox1.Items.Item(aaa)
        If txt = "__" Then
            Exit Sub
        End If
        txt = LCase(txt)
        ListBox1.Items.RemoveAt(aaa)
        ListBox1.Items.Insert(aaa, "__")
        DrawLetter(txt)
        Button1.Enabled = False
        If CorrectGuesses = CurWord.Length Then
            Panel1.Visible = True
            Button1.Enabled = False
            ListBox1.Enabled = False
            FinishMessages.Win(CorrectGuesses, BadGuesses, CurWord)
            RecordsFRM.Show()
        ElseIf BadGuesses >= 8 Then
            Panel1.Visible = True
            Button1.Enabled = False
            ListBox1.Enabled = False
            FinishMessages.lose(CorrectGuesses, BadGuesses, CurWord)
            RecordsFRM.Show()
        End If
    End Sub


    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedItem = "__" Then
            Button1.Enabled = False
            Exit Sub
        Else : Button1.Enabled = True
        End If

    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        NewWord()
    End Sub

    Private Sub AboutHangManToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutHangManToolStripMenuItem.Click
        FRmAbout.Show()
    End Sub

    ''' <summary>
    ''' update the stats in the panel
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub Panel1_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.VisibleChanged
        Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim b1 As New LinearGradientBrush(New Rectangle(0, 0, Panel1.Width, Panel1.Height), Color.Black, Color.White, LinearGradientMode.BackwardDiagonal)
        Dim c1 As New ColorBlend
        c1.Colors = New Color() {Color.LightBlue, Color.Cyan, Color.Transparent, Color.Peru}
        c1.Positions = New Single() {0.0, 0.3, 0.65, 1.0}
        b1.InterpolationColors = c1
        g.FillRectangle(b1, New Rectangle(0, 0, Panel1.Width, Panel1.Height))
        Dim p1 As New Pen(Color.DarkGoldenrod, 1)
        Dim p2 As New Pen(Color.Goldenrod, 1)
        Dim p3 As New Pen(Color.Gold, 1)
        g.DrawRectangle(p3, New Rectangle(0, 0, Panel1.Width - 1, Panel1.Height - 1))
        g.DrawRectangle(p2, New Rectangle(1, 1, Panel1.Width - 3, Panel1.Height - 3))
        g.DrawRectangle(p1, New Rectangle(2, 2, Panel1.Width - 5, Panel1.Height - 5))
        Panel1.BackgroundImage = bmp
        lblCorrect.Text = CorrectGuesses
        lblwrong.Text = BadGuesses
        lblpercent.Text = FormatPercent(CorrectGuesses / (CorrectGuesses + BadGuesses), 3)
        perc = CorrectGuesses / (BadGuesses + CorrectGuesses)
        perc = perc * 100
        'MsgBox(perc)
        'Exit Sub
        RecordsFRM.Hide()
        Select Case perc
            Case Is > My.Settings.Score1
                My.Settings.Score5 = My.Settings.Score4
                My.Settings.Score4 = My.Settings.Score3
                My.Settings.Score3 = My.Settings.Score2
                My.Settings.Score2 = My.Settings.Score1

                My.Settings.Name5 = My.Settings.Name4
                My.Settings.Name4 = My.Settings.Name3
                My.Settings.Name3 = My.Settings.Name2
                My.Settings.Name2 = My.Settings.Name1
                My.Settings.Score1 = perc
                My.Settings.Name1 = InputBox("Congratulations! You got the HIGH SCORE for Hang Man!!" & (Chr(13)) & "Please enter your name below.", "High Score")
                RecordsFRM.Index = 1
            Case Is > My.Settings.Score2
                My.Settings.Score5 = My.Settings.Score4
                My.Settings.Score4 = My.Settings.Score3
                My.Settings.Score3 = My.Settings.Score2
                'My.Settings.Score2 = My.Settings.Score1

                My.Settings.Name5 = My.Settings.Name4
                My.Settings.Name4 = My.Settings.Name3
                My.Settings.Name3 = My.Settings.Name2
                'My.Settings.Name2 = My.Settings.Name1
                My.Settings.Score2 = perc
                My.Settings.Name2 = InputBox("Congratulations! You got the SECOND high score for Hang Man!!" & (Chr(13)) & "Please enter your name below.", "High Score")
                RecordsFRM.Index = 2
            Case Is > My.Settings.Score3
                My.Settings.Score5 = My.Settings.Score4
                My.Settings.Score4 = My.Settings.Score3
                'My.Settings.Score3 = My.Settings.Score2
                'My.Settings.Score2 = My.Settings.Score1

                My.Settings.Name5 = My.Settings.Name4
                My.Settings.Name4 = My.Settings.Name3
                'My.Settings.Name3 = My.Settings.Name2
                'My.Settings.Name2 = My.Settings.Name1
                My.Settings.Score3 = perc
                My.Settings.Name3 = InputBox("Congratulations! You got the THIRD high score for Hang Man!!" & (Chr(13)) & "Please enter your name below.", "High Score")
                RecordsFRM.Index = 3
            Case Is > My.Settings.Score4
                My.Settings.Score5 = My.Settings.Score4
                'My.Settings.Score4 = My.Settings.Score3
                'My.Settings.Score3 = My.Settings.Score2
                'My.Settings.Score2 = My.Settings.Score1

                My.Settings.Name5 = My.Settings.Name4
                'My.Settings.Name4 = My.Settings.Name3
                'My.Settings.Name3 = My.Settings.Name2
                'My.Settings.Name2 = My.Settings.Name1
                My.Settings.Score4 = perc
                My.Settings.Name4 = InputBox("Congratulations! You got the Fourth high score for Hang Man!!" & (Chr(13)) & "Please enter your name below.", "High Score")
                RecordsFRM.Index = 4
            Case Is > My.Settings.Score5
                'My.Settings.Score5 = My.Settings.Score4
                'My.Settings.Score4 = My.Settings.Score3
                'My.Settings.Score3 = My.Settings.Score2
                'My.Settings.Score2 = My.Settings.Score1

                'My.Settings.Name5 = My.Settings.Name4
                'My.Settings.Name4 = My.Settings.Name3
                'My.Settings.Name3 = My.Settings.Name2
                'My.Settings.Name2 = My.Settings.Name1
                My.Settings.Score5 = perc
                My.Settings.Name5 = InputBox("Congratulations! You got the fifth high score for Hang Man!!" & (Chr(13)) & "Please enter your name below.", "High Score")
                RecordsFRM.Index = 5
            Case Else

        End Select
        'RecordsFRM.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        NewWord()
    End Sub

    Private Sub pb1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pb1.Click
        tbCheat.Focus()
    End Sub

    Private Sub pb1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles pb1.SizeChanged
        Dim bmp As New Bitmap(pb1.Width, pb1.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim path As New GraphicsPath
        path.AddRectangle(New Rectangle(0, 0, bmp.Width, bmp.Height))
        Dim b1 As New PathGradientBrush(path)
        b1.CenterPoint = New Point(pb1.Width / 2, pb1.Height / 2)
        b1.CenterColor = Color.Tomato
        b1.SurroundColors = New Color() {Color.Transparent}
        g.FillRectangle(b1, New Rectangle(0, 0, bmp.Width, bmp.Height))
        pb1.BackgroundImage = bmp
    End Sub

    Private Sub RecordsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecordsToolStripMenuItem.Click
        RecordsFRM.Show()
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        RecordsFRM.Show()
    End Sub

    Private Sub tbCheat_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tbCheat.TextChanged
        If tbCheat.Text = "show the current word" Then
            txtWord.Visible = True
        Else : txtWord.Visible = False
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
        OptionsFRM.Show()
    End Sub
End Class
